package cn.hxlc.tie.demo.modular.ljt.equipment.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.error.MdmExceptionEnum;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.EquipmentGroup;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.Facility;
import cn.hxlc.tie.demo.modular.ljt.equipment.mapper.EquipmentMapper_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.mapper.FacilityMapper_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.params.EquipmentGroupParam;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.EquipmentService_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.util.NumberUtil;
import cn.hxlc.tie.model.exp.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EquipmentServiceImpl_lt extends ServiceImpl<EquipmentMapper_lt, EquipmentGroup> implements EquipmentService_lt {

    @Autowired
    private FacilityMapper_lt facilityMapper;

    @Override
    public List<Map<String, Object>> queryTree() {
        List<Map<String,Object>> treeMaps =
                this.listMaps(new QueryWrapper<EquipmentGroup>().select("eid as nodeId","ename as nodeName","eparentid as nodePid"));
        //装配根节点
        /*Map<String,Object> rootNode = MapBuilder.create(new HashMap<String,Object>())
                .put("nodeId",ROOT_ID)
                .put("nodeName","设备分类")
                .put("nodePid",ROOT_PID).build();
        treeMaps.add(rootNode);*/

        return treeMaps;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void TreeCategoryAdd(EquipmentGroupParam param) {
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getEnumber(),param.getEid());
        EquipmentGroup category = getEntity(param);
        try {
            this.save(category);
        } catch (Exception e) {
            throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"左树新增失败!");
        }
    }

    @Override
    public EquipmentGroup selectById(String fid) {
        return this.baseMapper.selectById(fid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEquipment(EquipmentGroupParam param) {
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getEnumber(),param.getEid());
        EquipmentGroup category = getEntity(param);
        try {
            this.baseMapper.updateById(category);
        } catch (Exception e) {
            throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"编辑失败");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String fid) {
        QueryWrapper<EquipmentGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("eparentid",fid);
        Integer count = this.baseMapper.selectCount(wrapper);
        if (count >= 1){
            throw new ServiceException(MdmExceptionEnum.GROUPHAS_ERROR.getCode(),"分组下存在数据!");
        }else {
            QueryWrapper<Facility> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fgroupid",fid);
            Integer integer = facilityMapper.selectCount(queryWrapper);
            if (integer >= 1){
                throw new ServiceException(MdmExceptionEnum.GROUPHAS_ERROR.getCode(),"分组下存在数据");
            }else{
                try {
                    this.baseMapper.deleteById(fid);
                } catch (Exception e) {
                    throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"删除失败");
                }

            }
        }
    }

    private EquipmentGroup getEntity(EquipmentGroupParam param) {
        EquipmentGroup category =new EquipmentGroup();
        BeanUtil.copyProperties(param, category);
        return category;
    }


}
